#include "SoundSystem.h"

SoundSystem*	SoundSystem::GetInstance( void )
{
	//Store an instance in global memory
	static SoundSystem s_Instance;

	//Return the instance
	return &s_Instance;
}

SoundSystem::SoundSystem(void)
{
	result = System_Create(&fmodSystem);
	result = fmodSystem->init(100,FMOD_INIT_NORMAL,0);
	
	LoadSound("DST-AngryRobotIII","mp3");
	LoadSound("pickup1","mp3");
}

SoundSystem::~SoundSystem(void)
{
}

FMOD::Sound * SoundSystem::GetSound(std::string name)
{
	return m_hmSounds[name];
}

void SoundSystem::playSound(std::string name, bool isLooping)
{
	FMOD::Sound * sound = GetSound(name);
	if(sound!=nullptr)
	{
		result = fmodSystem->playSound(FMOD_CHANNEL_FREE,sound,false,&channel);
		sound->setMode(isLooping?FMOD_LOOP_NORMAL:FMOD_LOOP_OFF);
	}
}

void SoundSystem::StopSounds()
{
	channel->stop();
}

void SoundSystem::SetVolume(float _volume)
{
	if(_volume > 1.0f)
		_volume = 1.0f;
	else if(_volume <= 0.0f)
		_volume = 0.0f;
	channel->setVolume(_volume);
}
float SoundSystem::GetVolume()
{
	float fVolume;
	channel->getVolume(&fVolume);
	return fVolume;
}

void SoundSystem::PauseSound(std::string name)
{
	FMOD::Sound * sound = GetSound(name);

	if(sound!=nullptr)
	{
		result = fmodSystem->playSound(FMOD_CHANNEL_FREE,sound,false,&channel);
		channel->stop();
	}
}

void SoundSystem::LoadSound(std::string name,std::string ext)
{
	std::string fileName = "sounds/";
	fileName+=name;
	fileName+=".";
	fileName+=ext;
	FMOD::Sound * sound;
	result = fmodSystem->createSound(fileName.c_str(),FMOD_DEFAULT,0,&sound);
	m_hmSounds.insert(std::pair<std::string,FMOD::Sound*>(name,sound));
}

void SoundSystem::UnloadSound(std::string name)
{
	m_hmSounds.erase(name);
}

void SoundSystem::UnloadAll()
{	
	m_hmSounds.clear();
}

void SoundSystem::Update()
{
	result = fmodSystem->update();
}

